package shared;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * 
 */

/**
 * @author Sefverl
 *
 */
public class XMLModem 
{
	private Document docu;
	private TaskObject taskObject;
	private TaskQueue queue = TaskQueue.getInstance();
	
	public XMLModem()
	{
		
	}
	
	private void parseXML()
	{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		
		try
		{
			DocumentBuilder db = dbf.newDocumentBuilder();
			docu = db.parse("coord.xml");
		}
		catch (ParserConfigurationException pce) 
		{
			// TODO: handle exception
		}
		catch(SAXException sax)
		{
			
		}
		catch(IOException io)
		{
			
		}
	}
	
	public void parseDocument(Document dom)
	{
		Element root = dom.getDocumentElement();
		
		NodeList nl = root.getChildNodes();
		
		if(nl == null || nl.getLength() <= 0) return;
		
		NodeList coords = root.getElementsByTagName("coord");		
		NodeList actions = root.getElementsByTagName("action");
		
		taskObject = new TaskObject();
		
		generateCoordObjects(coords);
		
		queue.pushTask(taskObject);
	}
	
	private CoordObject generateCoordObject(String x, String y, String player)
	{
		int xInt = Integer.parseInt(x.trim());
		int yInt = Integer.parseInt(y.trim());
		
		return new CoordObject(xInt, yInt, player.trim());
	}
	
	private void generateCoordObjects(NodeList list)
	{
		String contentX = "",contentY = "",nameStr = "";
		
		for(int i = 0; i<list.getLength(); i++)
		{
			Element element = (Element) list.item(i);
			NodeList xCoord = element.getElementsByTagName("x");
			
			for(int j = 0; j<xCoord.getLength(); j++)
			{
				Element textX = (Element) xCoord.item(j);
				contentX = textX.getTextContent();
			}
			
			NodeList yCoord = element.getElementsByTagName("y");
			
			for(int k = 0; k<yCoord.getLength(); k++)
			{
				Element textY = (Element) yCoord.item(k);
				contentY = textY.getTextContent();
			}
			
			NodeList player = element.getElementsByTagName("player");
			
			for(int l = 0; l<player.getLength(); l++)
			{
				NodeList names = ((Element)player.item(l)).getElementsByTagName("name");
				
				for(int m = 0; m<names.getLength(); m++)
				{
					Element name = (Element) names.item(m);
					nameStr = name.getTextContent();
				}
			}
			
			CoordObject obj = generateCoordObject(contentX, contentY, nameStr);
			taskObject.addCoordObject(obj);
		}		
		
	}
	
	private void generateActionObjects(NodeList list)
	{
		
	}
	
	public Document generateDocument(TaskObject obj)
	{
		Document doc = null;
		
		DocumentBuilderFactory docBuild = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = docBuild.newDocumentBuilder();
			doc = builder.newDocument();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Element root = doc.createElement("root");
		doc.appendChild(root);
		
		if(obj.coordsExist()) parseCoordObjects(obj.getCoordObjects(), root, doc);
		if(obj.actionsExist()) parseActionObjects(obj.getActionObjects(), root, doc);
		
		return doc;
	}
	
	private void parseCoordObjects(ArrayList<CoordObject> coords, Element root, Document doc)
	{
		for(int i = 0; i<coords.size(); i++)
		{
			CoordObject co = coords.get(i);
			Element coord = doc.createElement("coord");
			
			Element coordX = doc.createElement("x");
			coordX.setTextContent(""+co.getX());
			Element coordY = doc.createElement("y");
			coordY.setTextContent(""+co.getY());
			
			
			Element coordPlayer = doc.createElement("player");
			
			Element playerName = doc.createElement("name");
			playerName.setTextContent(co.getPlayerName());
			
			coordPlayer.appendChild(playerName);
			
			coord.appendChild(coordX);
			coord.appendChild(coordY);
			coord.appendChild(coordPlayer);
			
			root.appendChild(coord);
		}
	}
	
	private void parseActionObjects(ArrayList<ActionObject> actions,Element root,Document doc)
	{
		
	}
}
